const bt = require('./bt');

//二叉树的先序遍历(递归)
const preorder_1 = (root) => {
    if (!root){return;}
    console.log(root.val);
    preorder_1(root.left);
    preorder_1(root.right);
};

preorder_1(bt);

//二叉树的先序遍历(非递归)
const preorder_2 = (root) => {
    if (!root) return;
    const stack = [root];
    while (stack.length){
        const n = stack.pop();
        console.log(n.val);
        //先放入右子节点，通过栈结构，后进先出
        if (n.right) stack.push(n.right);
        if (n.left) stack.push(n.left);
    }
};